﻿/****************************************************************************
*                                                                           *
* Copyright (c) Ionescu M. Marius. All rights reserved(2010).               *
*                                                                           *
****************************************************************************/

// (c) Copyright Romanian Software Company.
// Last Change: 13.10.2009 - by Ionescu Marius.
// All rights reserved.

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Collections;
using System.Data;
using AjaxControlToolkit;

namespace NS_Controls
{
	public class DateTimeField : BoundField
	{
		public bool FullDateTime
		{
			get { return (bool)(this.ViewState["FullDateTime"] ?? false); }
			set { this.ViewState["FullDateTime"] = value; }
		}

        public string sufix
        {
            get { return (string)(this.ViewState["sufix"] ?? ""); }
            set { this.ViewState["sufix"] = value; }
        }

		public string DateTimeFormat
		{
			get { return (string)(this.ViewState["DateTimeFormat"] ?? "yyyy-MM-dd HH:mm:ss"); }
			set { this.ViewState["DateTimeFormat"] = value; }
		}

		protected override void InitializeDataCell(DataControlFieldCell cell, DataControlRowState rowState)
		{
			if (!this.ReadOnly && (0 != (rowState & DataControlRowState.Edit) || 0 != (rowState & DataControlRowState.Insert)))
			{
				TextBox txb = new TextBox();
				txb.ID = "txf" + sufix;
				txb.ToolTip = this.HeaderText;
				txb.BackColor = System.Drawing.Color.White;

				CalendarExtender cal = new CalendarExtender();
                cal.TargetControlID = txb.ID;
				cal.PopupButtonID = "imgCalEnd";
				cal.Format = DateTimeFormat;

				FilteredTextBoxExtender fte = new FilteredTextBoxExtender();
                fte.TargetControlID = txb.ID;
				fte.ValidChars = DateTimeFormat.Contains("MMM") ? " /-.:0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" :
					" /-.:0123456789";

				if (!String.IsNullOrEmpty(this.DataField) && 0 != (rowState & DataControlRowState.Edit))
				{
					txb.DataBinding += new EventHandler(HandleDataBinding);
				}

				cell.Controls.Add(txb);
				cell.Controls.Add(cal);
				cell.Controls.Add(fte);

			}
			else if (!String.IsNullOrEmpty(this.DataField))
			{
				Label lbl = new Label();
				lbl.DataBinding += new EventHandler(HandleDataBinding);
				cell.Controls.Add(lbl);
			}
		}

		public override void ExtractValuesFromCell(System.Collections.Specialized.IOrderedDictionary dictionary, DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
		{
			if (cell.HasControls() && cell.Controls[0] is TextBox)
			{
				TextBox txb = (TextBox)cell.Controls[0];
				if (txb.Text != string.Empty)
					dictionary[this.DataField] = txb.Text;
				else
					dictionary[this.DataField] = System.DBNull.Value;
			}
		}

		private void HandleDataBinding(object sender, EventArgs e)
		{
			Control ctrl = (Control)sender;

			string val = null;
			if (ctrl.NamingContainer != null)
			{
				object rawVal = GetValue(ctrl.NamingContainer);
				val = rawVal is DateTime ? ((DateTime)rawVal).ToString(DateTimeFormat) :
					(null == rawVal ? null : rawVal.ToString());
			}

			if (ctrl is TextBox)
			{
				TextBox txf = (TextBox)ctrl;
				if ((val != string.Empty) && !FullDateTime)
					if (val.LastIndexOf(' ') > 0) //just in case, although it shouldn't happen...
						val = val.Substring(0, val.LastIndexOf(' '));
				txf.Text = val;
			}

			else if (ctrl is Label)
			{
				Label lbl = (Label)ctrl;

				TextBox temp = new TextBox();
				temp.Text = val;
				temp.Visible = false;

				lbl.Controls.Add(temp);
				temp.DataBind();

				lbl.Text = temp.Text;
			}
		}
	}
}
